package com.ys.controller.web.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.business.TpAmazonShop;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/business/tpAmazonShop")
public class WebTpAmazonShopController {

	@Autowired
	private CommonApiService<TpAmazonShop> commonApiService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("tpAmazonShop:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TP_AmazonShop, new ArrayList<Map<String, Object>>());
			
			return commonApiService.toAdd(model, new TpAmazonShop(), MenuId.TP_AmazonShop, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 修改
	 */
	@RequiresPermissions("tpAmazonShop:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		TpAmazonShop entity = new TpAmazonShop().setId(id).queryFirst();
		
		Map<String, Object> extraParam = new TpAmazonShop().setId(id)
		    	
		    	.addLeftJoin("TB_Site B ON B.Id = A.SiteId")
		    		
				.addLeftJoin("TP_AmazonRegion C ON C.Code = B.RegionCode")
				.addField("C.Code regionCode")
				.addField("C.Name regionName")
				
				.addLeftJoin("TB_Platform D ON D.Code = C.PlatformCode")
				.addField("D.Code platformCode")
				.addField("D.Name platformName")
				
				.addLeftJoin("TP_AmazonSeller E ON E.id = A.amazonSellerId")
				.addField("E.Vgsbm vgsbm")
				.addField("E.Vgsjc vgsjc")
				
				.clearFieldsSource()
				.queryFirstMap();

		try {
			// 子列表配置信息
			List<Map<String, Object>> sonlist = new TpAmazonShop()
					.setAmazonSellerId(entity.getAmazonSellerId())
			    	
			    	.addLeftJoin("TB_Site B ON B.Id = A.SiteId")
			    	
			    	.addLeftJoin("TB_Country B1 ON B1.Id = B.CountryId")
					.addField("B1.Code countryCode")
					.addField("B1.Name countryName")
					
					.queryMap();
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TP_AmazonShop, sonlist);
			
			return commonApiService.toEdit(model, id, new TpAmazonShop(), MenuId.TP_AmazonShop, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 详情页
	 */
	@RequiresPermissions("tpAmazonShop:view")
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		Map<String, Object> extraParam = new TpAmazonShop().setId(id)
				.addLeftJoin("TP_AmazonRegion B ON B.Code = A.RegionCode")
				.addField("B.Name regionName")
				
				.addLeftJoin("TB_Platform B1 ON B1.Code = B.PlatformCode")
				.addField("B1.Code platformCode")
				.addField("B1.Name platformName")
				
				.clearFieldsSource()
				.queryFirstMap();

		try {
			return commonApiService.toDetail(model, id, new TpAmazonShop(), MenuId.TP_AmazonShop, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 刷新本地近14天订单数量
	 */
	@GetMapping("/toUpdateNumLocal")
	public String toUpdateNumLocal(Model model, HttpServletRequest request, HttpServletResponse response){
		
		return "business/tpAmazonShop/toUpdateNumLocal";
	}
	
	/*
	 * 刷新店铺订单（单店铺）
	 */
	@GetMapping("/toRefreshOrderByShop")
	public String toRefreshOrderByShop(Model model, HttpServletRequest request, HttpServletResponse response,
			String guid){

		TpAmazonShop tpAmazonShop = new TpAmazonShop().setGuid(guid).queryFirst();
		
		String shopName = tpAmazonShop.getShopName();
		ToRefreshOrderByShopDto dto = new ToRefreshOrderByShopDto(shopName);
		model.addAttribute("dto", dto);
		return "business/tpAmazonShop/toRefreshOrderByShop";
	}
	@Data
	@AllArgsConstructor
	private static class ToRefreshOrderByShopDto{
		private String nameShop;
	}
}
